// iViewComponent/date-time-picker/index.js
import util from '../../utils/util.js'

let date = new Date()

const years = []
const months = []
//大月日期
const days1 = []
//平月日期
const days2 = []
//闰年2月日期
const days3 = []
//平年年2月日期
const days4 = []

//
const hours = []
const minutes = []
const seconds = []

//
const bigMonths = [1, 3, 5, 7, 8, 10, 12]

for (let i = 1990; i <= (date.getFullYear() + 10); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)

}

for (let i = 1; i <= 31; i++) {
  if (i <= 28) {
    days4.push(i)
  }
  if (i <= 29) {
    days3.push(i)
  }
  if (i <= 30) {
    days2.push(i)
  }
  days1.push(i)
}

for (let i = 0; i < 60; i++) {
  if (i <= 24) hours.push(i);
  minutes.push(i)
  seconds.push(i)
}


Component({
  behaviors: ['wx://form-field'],
  /**
   * 组件的属性列表
   */
  properties: {

    title: {
      type: String
    },
    name: {
      type: String
    },
    value: {
      type: String
    },
    key: {
      type: String
    },
    error: {
      type: Boolean
    },
    placeholder: {
      type: String,
      value: '请点击选择时间'
    }
  },

  created(e){
    // console.log('created',e)
  },
  attached(e) {
    // console.log('attached', e)
  },
  ready(e) {
    // console.log('ready', e)
  },
  /**
   * 组件的初始数据
   */
  data: {
    years: years,
    months: months,
    days: days1,
    hours: hours,
    minutes: minutes,
    seconds: seconds,
    showPop: false,
    pickerValue: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**将value转变为pickerValue */
    changeDateTimeValue() {
      let d = this.data.value ? new Date(this.data.value) : new Date()
      let yy = d.getFullYear()
      let MM = d.getMonth() + 1
      let dd = d.getDate()
      let HH = d.getHours()
      let mm = d.getMinutes()
      let ss = d.getSeconds()
      this.checkDayArray(yy, MM)
      // console.log(yy, MM, dd, HH, mm, ss)
      this.initPickerValue(yy, MM, dd, HH, mm, ss)
    },
    /**赋值初始化 pickerValue*/
    initPickerValue(yy, MM, dd, HH, mm, ss) {
      let pickerValue = []
      let yearIndex = this.data.years.indexOf(yy)
      pickerValue.push(yearIndex)
      let monthIndex = this.data.months.indexOf(MM)
      pickerValue.push(monthIndex)
      let dayIndex = this.data.days.indexOf(dd)
      pickerValue.push(dayIndex)
      let hIndex = this.data.hours.indexOf(HH)
      pickerValue.push(hIndex)
      let mIndex = this.data.minutes.indexOf(mm)
      pickerValue.push(mIndex)
      let sIndex = this.data.seconds.indexOf(ss)
      pickerValue.push(sIndex)
      this.setData({
        pickerValue: pickerValue
      })
    },
    /**检查Days数组 */
    checkDayArray(y, m) {
      let days = []
      if (m === 2) {
        if (((y % 4) == 0) && ((y % 100) != 0) || ((y % 400) == 0)) {
          if (this.data.days.length === days3.length) {
            return;
          } else {
            days = days3
          }
        } else {
          if (this.data.days.length === days4.length) {
            return;
          } else {
            days = days4
          }
        }
      } else {
        if (bigMonths.indexOf(m) != -1) {
          if (this.data.days.length === days1.length) {
            return;
          } else {
            days = days1
          }
        } else {
          if (this.data.days.length === days2.length) {
            return;
          } else {
            days = days2
          }
        }
      }
      this.setData({
        days
      })
    },
    handleClosePop() {
      this.setData({
        showPop: false
      })
    },
    clickDate() {
      this.changeDateTimeValue()
      this.setData({
        showPop: true
      })
    },
    /**处理监听Picker滚动变化 */
    bindChange(e) {
      // console.log('bindChange', e)
      const {
        detail
      } = e
      const {
        value
      } = detail
      let year = this.data.years[value[0]]
      let month = this.data.months[value[1]]
      // let day = this.data.days[value[2]]
      // let h = this.data.hours[value[3]]
      // let m = this.data.minutes[value[4]]
      // let s = this.data.seconds[value[5]]

      this.checkDayArray(year, month)
      // let v = year+'-'+month+'-'+day+' '+h+':'+m+':'+s
      // let date = new Date(year,month-1,day,h,m,s)
      // let v =  util.formatTime(date)
     
      this.setData({
        pickerValue: value
      })
    },
    /**确认选择时间 */
    confirmValueChange() {
      let value = this.data.pickerValue
      let year = this.data.years[value[0]]
      let month = this.data.months[value[1]]
      let day = this.data.days[value[2]]
      let h = this.data.hours[value[3]]
      let m = this.data.minutes[value[4]]
      let s = this.data.seconds[value[5]]
      // console.log('kjsish', year, month, day, h, m, s)
      let date = new Date(year, month - 1, day, h, m, s)
      let v = util.formatTime(date)
      this.setData({
        value: v,
        showPop: false
      })
      this.triggerEvent('change', {
        key: this.data.key,
        value: v
      });
    },
    // handleGetNowTime(){
    //   console.log('handleGetNowTime')
    //   let v = util.formatTime(new Date())
    //   this.setData({
    //     value: v
    //   })
    // }
  }
})